package org.kjk.board;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class BoardSearchController extends AbstractController {
	private BoardDAOImpl dao;

	@SuppressWarnings("unchecked")
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("call board service");
		String queryType = request.getParameter("type");
		String queryField = request.getParameter("field");

		String sql = null;
		List<BoardDTO> result = null;
		String[] param = null;
		if (queryType == null) {
			sql = "select * from board order by serial desc";
		} else if (queryType.trim().equals("1")) {
			sql = "select * from board where subject like '%?%' order by serial desc";
			param = new String[] { queryField };
		} else if (queryType.trim().equals("2")) {
			sql = "select * from board where content like '%?%' order by serial desc";
			param = new String[] { queryField };
		} else {
			sql = "select * from board order by serial desc";
		}

		result = (List<BoardDTO>) dao.select(sql, param);
		return new ModelAndView("board/board.jsp", "board", result);
	}

	public void setDao(BoardDAOImpl dao) {
		this.dao = dao;
	}
}
